package cn.xbtop.blog.controller;

import cn.xbtop.blog.pojo.Blog;
import cn.xbtop.blog.service.BlogService;
import cn.xbtop.blog.utils.PageBean;
import cn.xbtop.blog.utils.Result;
import cn.xbtop.blog.vo.BlogVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/4/11 20:46
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 添加
     *
     * @param blog
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<Object> save(@RequestBody Blog blog) {
        blogService.insert(blog);
        return new Result<>("添加成功！");
    }

    /**
     * 后台使用
     * 根据id查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Result<Object> findById(@PathVariable String id) {
        Blog blog = blogService.selectByPrimaryKey(id);
        return new Result<>(blog);
    }

    /**
     * 修改
     *
     * @param blog
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result<Object> update(@RequestBody Blog blog) {
        blogService.updateByPrimaryKey(blog);
        return new Result<>("修改成功！");
    }

    /**
     * 根据id阅读
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/read/{id}", method = RequestMethod.GET)
    public Result<BlogVo> read(@PathVariable String id) {
        BlogVo blogVo = blogService.readById(id);
        return new Result<>(blogVo);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result<Object> delete(@PathVariable String id) {
        blogService.deleteByPrimaryKey(id);
        return new Result<>("删除成功！");
    }

    /**
     * 查询所有博客
     *
     * @return
     */
    @RequestMapping(value = "/findAllBlog", method = RequestMethod.GET)
    public Result<Object> findAllBlog() {
        List<Blog> list = blogService.findAllBlog();
        return new Result<>(list);
    }

    /**
     * 分页查询所有博客
     *
     * @return
     */
    @RequestMapping(value = "/findByPage", method = RequestMethod.POST)
    public Result<PageBean<BlogVo>> findByPage(@RequestBody PageBean<BlogVo> pageBean) {
        String sortColumn = pageBean.getSortColumn();
        //判断是否为空
        if (StringUtils.isNotBlank(sortColumn)) {
            //排序列不为空
            String[] sortColumns = {"blog_goods", "blog_read", "blog_collection" +
                    "bype_name", "blog_comment", "created_time", "update_time"};
            List<String> sortlist = Arrays.asList(sortColumns);

            if (!sortlist.contains(sortColumn.toLowerCase())) {
                return new Result<>(40005, "排序参数不合法！");
            }
        }
        pageBean = blogService.findByPage(pageBean);
        return new Result<>(pageBean);
    }
}
